package com.example.the_breakfast_club;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Screen for new item creation.
 * @author Team 19
 */
public class NewItem2Activity extends Activity {

	private String category_spinner[];
	private DatabaseHandler db;
	private String mName;
	private String mDesc;
	private String mLocation;
	private EditText mNameView;
	private EditText mDescView;
	private EditText mLocationView;
	
	/**
	 * Constructor for any activity.
	 * @param savedInstanceState, retained state information
	 */
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_newitem2);
		db = DatabaseHandler.getInstance(this);
		
		mNameView = (EditText) findViewById(R.id.item_name);
		mDescView = (EditText) findViewById(R.id.item_description);
		mLocationView = (EditText) findViewById(R.id.item_location);
		
		category_spinner = new String[3];
		category_spinner[0] = "Keepsake";
		category_spinner[1] = "Heriloom";
		category_spinner[2] = "Miscellaneous";
		Spinner s = (Spinner) findViewById(R.id.item_category);
		ArrayAdapter adapter = new ArrayAdapter(this,
		android.R.layout.simple_spinner_item, category_spinner);
		s.setAdapter(adapter);
		
		findViewById(R.id.cancel_item_button).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(NewItem2Activity.this, Results2Activity.class);
				NewItem2Activity.this.startActivity(intent);
			}
		});
		
		findViewById(R.id.accept_item_button).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				addNewItem();
				Intent intent = new Intent(NewItem2Activity.this, Results2Activity.class);
				NewItem2Activity.this.startActivity(intent);
			}
		});
	}

	/**
	 * Adds a new item to the database, taking values from the text fields.
	 */
	public void addNewItem(){
		mName = mNameView.getText().toString();
		mDesc = mDescView.getText().toString();
		mLocation = mLocationView.getText().toString();
		db.addItem(new Item(mName, mDesc, mLocation, 1, true, false));
	}
	
	/**
	 * Inflates the specified menu. 
	 * Initializes the contents of the Activity's standard options menu.
	 * @param menu, menu to be inflated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_results, menu);
		return true;
	}
}
